<?php

class FxApp extends MallbaseApp
{
	function index()
	{
		$db=&db();
		$sql="select * from zys_dictionary where d_code='boxtype' and d_del=1 order by d_order";
		$type=$db->getAll($sql);

		$this->assign('type', $type);

		$this->display('fx.html');
	}

	function cw()
	{
		$sdefaultDate = date("Y/m/d");
		$this->assign('start', $sdefaultDate);
		$this->display('cw.html');
	}

	function zdz()
	{
		$y=date("Y");
		$ys=array();
		for($i=0;$i<11;$i++){
			$yt['y']=$y-$i;
			$ys[]=$yt;
		}

		$weeks = date("W", mktime(0, 0, 0, 12, 30, $y));
		for($i=1;$i<=$weeks;$i++){
			$wt['w']=$i;
			$ws[]=$wt;
		}

		$this->assign('ys', $ys);
		$this->assign('ws', $ws);
		$this->assign('y', $y);
		$this->assign('w', date("W", gmtime()));

		$data=array('w0x'=>1,'w1x'=>1,'w2x'=>1,'w3x'=>1,'w4x'=>1,'w5x'=>1,'w6x'=>1,
		'w0m'=>1,'w1m'=>1,'w2m'=>1,'w3m'=>1,'w4m'=>1,'w5m'=>1,'w6m'=>1,'xa'=>7,'ma'=>7);

		$this->assign('data', $data);
		$this->display('zdz.html');
	}

	function zdy()
	{
		$y=date("Y");
		$ys=array();
		for($i=0;$i<11;$i++){
			$yt['y']=$y-$i;
			$ys[]=$yt;
		}

		for($i=1;$i<=12;$i++){
			$wt['w']=$i;
			$ws[]=$wt;
		}

		$this->assign('ys', $ys);
		$this->assign('ws', $ws);
		$this->assign('y', $y);
		$this->assign('w', date("m"));

		$data=array('xa'=>31,'ma'=>31);
		for($i=1;$i<=31;$i++){
			$data['x'][$i]=1;
			$data['m'][$i]=1;
				
			$t['d']=$i;
			$dddd[]=$t;
		}

		$this->assign('ddd', $dddd);

		$this->assign('data', $data);

		$this->display('zdy.html');
	}

	function cwz()
	{
		$y=date("Y");
		$ys=array();
		for($i=0;$i<11;$i++){
			$yt['y']=$y-$i;
			$ys[]=$yt;
		}

		$weeks = date("W", mktime(0, 0, 0, 12, 30, $y));
		for($i=1;$i<=$weeks;$i++){
			$wt['w']=$i;
			$ws[]=$wt;
		}

		$this->assign('ys', $ys);
		$this->assign('ws', $ws);
		$this->assign('y', $y);
		$this->assign('w', date("W", gmtime()));
		$this->display('cwz.html');
	}

	function cwy()
	{
		$y=date("Y");
		$ys=array();
		for($i=0;$i<11;$i++){
			$yt['y']=$y-$i;
			$ys[]=$yt;
		}

		for($i=1;$i<=12;$i++){
			$wt['w']=$i;
			$ws[]=$wt;
		}

		$this->assign('ys', $ys);
		$this->assign('ws', $ws);
		$this->assign('y', $y);
		$this->assign('w', date("m"));

		$this->display('cwy.html');
	}

	function hz()
	{
		$order=empty($_REQUEST['f']) ? 'MK_DD' : $_REQUEST['f'];
		$sort=empty($_REQUEST['s']) ? 'DESC' : $_REQUEST['s'];

		$this->assign('order', $order);
		$this->assign('sort', $sort);

		$sqldb=&sqldb();
		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);

		$db=&db();
		$sql="select depot,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		$page = $this->_get_pagen(10);

		$k = empty($_REQUEST['k']) ? '' : $_REQUEST['k'];

		$sdefaultDate = date("Y/m/d");
		$week_start=date("Y/m/d",strtotime(date("Y/m/d"))-30*24*60*60);
		$week_end=$sdefaultDate;

		$bgn_dd = empty($_REQUEST['bgn_dd']) ? $week_start : $_REQUEST['bgn_dd'];
		$end_dd = empty($_REQUEST['end_dd']) ? $week_end : $_REQUEST['end_dd'];
		$this->assign('start', $bgn_dd);
		$this->assign('end', $end_dd);

		$conditions = ' and 1=1 AND YW_TYPE=1';
		if($k){
			$conditions.=" and (a.FD_USR like '%".$k."%' or A.CUS_NO like '%".$k."%' or A.TD_NO like '%".$k."%')";
		}

		if($bgn_dd){
			$conditions.=" and A.MK_DD>='".$bgn_dd."'";
		}
		if($end_dd){
			$conditions.=" and A.MK_DD<='".$end_dd." 23:59:59'";
		}

		$sql="SELECT *
FROM (
select ROW_NUMBER() OVER (ORDER BY A.MK_DD desc) AS RowNumber,FDO_DD,COMP,A.CUS_NO,dbo.PT_UBOXINFO(A.FD_NO,'FDUSR','','','') FD_USR,SHIP_NO+OVOY SHIP_OVOY,TD_NO,XX,CC,QTY
  ,CASE WHEN ISNULL(DEL_FLAG,'')='T' THEN '已作废' WHEN ISNULL(FDZT,'')='B' THEN '放单中' WHEN ISNULL(FDZT,'')='G' THEN '改单' WHEN ISNULL(FDZT,'')='Y' THEN '放单异常' WHEN ISNULL(FDZT,'')='O' THEN '放单结束' ELSE '' END FDZT
  ,B.NAME SOUR,dbo.PT_FDFEE(a.fd_no,'D') DD_FEE,dbo.PT_FDFEE(a.fd_no,'L') LW_FEE,dbo.PT_FDFEE(a.fd_no,'T') TOT_FEE,CASE WHEN ISNULL(A.YC_FLAG,'')='Y' THEN YC_REM END REM ,A.REM YXYQ,A.MK_DD 
  from T_UBOXPLAN a left join CUST b on A.SOUR=B.CUS_NO
  WHERE A.USR='".$u['user_name']."' ".$conditions.") T_UBOXPLANPage
WHERE RowNumber > ".$page['s']." AND RowNumber <= ".$page['e']."
ORDER BY $order $sort";
		
		$plan=$sqldb->getAll($sql);
		$list=array();
		if($plan){
			foreach ($plan as $v){

				if($v['MK_DD ']){
					$v['MK_DD ']=gmstr2time($v['MK_DD ']);
				}

				if($v['FDO_DD']){
					$v['FDO_DD']=gmstr2time($v['FDO_DD']);
				}
				$list[]=$v;
			}
		}

		$sql="select count(1) from T_UBOXPLAN A where USR='".$u['user_name']."' and ISNULL(DEL_FLAG,'')!='T' ".$conditions;
		$c=$sqldb->getOne($sql);

		$page['item_count'] = $c;
		$this->_format_pageweb($page);
		$this->assign('page_info', $page);

		$this->assign('plan', $list);

		$this->display('hz.html');
	}

	function getsl(){
		$t = empty($_REQUEST['t']) ? '' : $_REQUEST['t'];

		$db=&db();
		$sql="select code,CONCAT(code,' ',sname) as sname from zys_shippingline where (code like '%".$t."%' or sname like '%".$t."%') and isuse2=1 order by code";
		$data=$db->getAll($sql);
		if($data){
			echo json_encode($data);
		}else{
			echo '[]';
		}
		exit();
	}

	function yd(){
		$sqldb=&sqldb();
		$CUS_NO = empty($_REQUEST['CUS_NO']) ? '' : $_REQUEST['CUS_NO'];

		$SHIP_OVOY= empty($_REQUEST['SHIP_OVOY']) ? '' : $_REQUEST['SHIP_OVOY'];
		$OVOY= empty($_REQUEST['OVOY']) ? '' : $_REQUEST['OVOY'];

		$REM= empty($_REQUEST['REM']) ? '' : $_REQUEST['REM'];

		$his= empty($_REQUEST['his']) ? '' : $_REQUEST['his'];
		if(empty($his)){
			$this->pons_error('info','您没有添加集装箱');
			exit();
		}
		$his=array_values($his);


		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);
		if(empty($uid)){
			$this->pons_error('info','您还没有登录，请先登录');
			exit();
		}

		$db=&db();
		$sql="select depot,del,company,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		if($u['del']){
			$this->pons_error('info','用户被禁用，如有疑问请联系客服');
			exit();
		}

		if(!$u['depot']){
			$this->pons_error('info','用户还没有被系统确认，无法申请');
			exit();
		}

		$sql="select * from T_SALM where SAL_NO='".$u['depot']."'";
		$salm=$sqldb->getRow($sql);

		if(!$salm){
			$this->pons_error('info','用户还没有被系统确认，无法申请');
			exit();
		}

		if(!$CUS_NO){
			$this->pons_error('info','船公司无效');
			exit();
		}

		$sql="select 1 from zys_shippingline where code='".$CUS_NO."'";
		$data=$db->getOne($sql);

		if(!$data){
			$this->pons_error('info','船公司无效');
			exit();
		}

		if(!$SHIP_OVOY){
			$this->pons_error('info','请填写船名');
			exit();
		}

		if(!$OVOY){
			$this->pons_error('info','请填写航次');
			exit();
		}

		foreach ($his as $v){
			if(empty($v['TD_NO'])){
				continue;
			}
			
			$XX= empty($v['XX']) ? '' : $v['XX'];
			$QTY= empty($v['QTY']) ? 0 : intval($v['QTY']);
			$TD_NO= empty($v['TD_NO']) ? '' : $v['TD_NO'];

			if(!$XX){
				$this->pons_error('info','请选择箱型');
				exit();
			}

			if(!$QTY){
				$this->pons_error('info','请输入箱量');
				exit();
			}

			if(!$TD_NO){
				$this->pons_error('info','请填写关单号');
				exit();
			}

			$sql="insert into T_UBOXPLAN(VC_NO,SHIP_NO,TD_NO,XX,QTY,REM,USR,COMP,COMP_NO,MK_DD,CUS_NO,OVOY,YW_TYPE)
		values('".$this->create_guidnew()."','".$SHIP_OVOY."','".$TD_NO."','".$XX."','".$QTY."',
		'".$REM."','".$u['user_name']."','".$salm['COMP']."','".$salm['COMPNO']."',
		'".local_date('Y-m-d H:i:s',gmtime())."','".$CUS_NO."','".$OVOY."',1)";
			$sqldb->query($sql);
		}

		$ret_url="index.php?app=fx";
		$this->pons_result($ret_url);
		exit();
	}

	function cancel(){
		$sqldb=&sqldb();
		$n= empty($_REQUEST['n']) ? '' : $_REQUEST['n'];
		if(empty($n)){
			$this->pons_error('info','要箱计划不存在');
			exit();
		}
		$u=$this->visitor->get("user_name");
		$sql="exec pt_uboxcheck '".$n."','".$u."'";
		$d=$sqldb->getRow($sql);

		if(empty($d)){
			$this->pons_error('info','操作失败,请重试');
			exit();
		}else{
			if($d['flag']=='F'){
				$this->pons_error('info',$d['ts']);
				exit();
			}
		}

		$this->pons_result('index.php?app=plan');
		exit();
	}
}

?>
